package com.spring.boot03.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spring.boot03.bean.Emp;
import com.spring.boot03.bean.ResultCode;
import com.spring.boot03.bean.ResultTemplate;
import com.spring.boot03.mapper.EmpMapper;
import com.spring.boot03.service.EmpService;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Cacheable(cacheNames = "emp",key = "#id")
    @Override
    public ResultCode<Emp> find(int id) {
       Emp emp=baseMapper.selectById(id);
       return ResultTemplate.success(emp);
    }

    /*
    1.cacheNames和key必须和查询保持一致
    2.返回结果必须和查询保持一致
    3.1首先删除数据库中的数据
    3.2其次删除redis中指定的key
    3.3重新查询数据库中指定的数据
    3.4存放到redis中
     */
    @CachePut(cacheNames = "emp",key = "#id")
    public ResultCode<Emp> deleteEmp(int id){
        baseMapper.deleteById(id);
        return ResultTemplate.success(baseMapper.selectById(id));
    }
}
